package com.mybatisplus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mybatisplus.mapper.UserMapper;
import com.mybatisplus.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

/**
 * 项目名称：mybatis_plus_demo
 * 类 名 称：WrapperTeset
 * 类 描 述：TODO
 * 创建时间：2020/5/21
 * 创 建 人：Areli
 */
@SpringBootTest
public class WrapperTeset {
    @Autowired
    UserMapper userMapper;

    @Test// 查询name不为空的用户，并且邮箱不为空的用户，年龄大于等于12
    void test1(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("name").isNotNull("email").ge("age",12);
        userMapper.selectList(queryWrapper).forEach(System.out::println);
    }

    @Test// 查询名字
    void test2(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name","xpower");
        User user = userMapper.selectOne(wrapper);//查询一个数据，出现多个结果使用List 或者 Map
        System.out.println(user);
    }

    @Test// 查询年龄在 20 ~ 30 岁之间的用户
    void test3(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.between("age",20,30);
        Integer count = userMapper.selectCount(wrapper);
        System.out.println(count);
    }

    @Test// 模糊查询  查询名字中不包含e 且邮箱是以t开头的
    void test4(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.notLike("name","e").likeRight("email","t");
        List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
        maps.forEach(System.out::println);
    }

    @Test// 嵌套子查询
    void test5(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.inSql("id","select id from user where id < 3");
        List<Object> objects = userMapper.selectObjs(wrapper);
        objects.forEach(System.out::println);
    }

    @Test// 通过id进行排序
    void test6(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        List<User> users = userMapper.selectList(wrapper);
        users.forEach(System.out::println);
    }
}
